// DlgPick.cpp : implementation file
//

#include "stdafx.h"
#include "casino.h"
#include "DlgPick.h"
#include "robot.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgPick dialog


CDlgPick::CDlgPick(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgPick::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgPick)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CDlgPick::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgPick)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgPick, CDialog)
	//{{AFX_MSG_MAP(CDlgPick)
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
	ON_BN_CLICKED(IDC_GET, OnGet)
	ON_BN_CLICKED(IDC_SET, OnSet)
	ON_BN_CLICKED(IDC_PUSH, OnPush)
	ON_BN_CLICKED(IDC_POP, OnPop)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgPick message handlers
BOOL CDlgPick::OnInitDialog() 
{
	CDialog::OnInitDialog();
   
	POINT pos = Config::getInstance()->GetPos("idle");
    if(pos.x>0 && pos.y>0)
    {
        SetDlgItemInt(IDC_READ_X, pos.x);
        SetDlgItemInt(IDC_READ_Y, pos.y);
    }

    COLORREF color = Config::getInstance()->GetColor("idle");
    if(color > 0)
    {
	    SetDlgItemInt(IDC_READ_COLOR, color);
    }

    SetDlgItemText(IDC_N, "");

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgPick::OnLButtonDown(UINT nFlags, CPoint point) 
{

   CDialog::OnLButtonDown(nFlags, point);
}

void CDlgPick::OnRButtonDown(UINT nFlags, CPoint point) 
{
	POINT pos;
    CRobot::getIntance()->GetIdlePos(pos);

    SetDlgItemInt(IDC_CURRENT_X, pos.x);
    SetDlgItemInt(IDC_CURRENT_Y, pos.y);

	CDialog::OnRButtonDown(nFlags, point);
}

void CDlgPick::ReFormatBetName(CString &str)
{
    if(str.IsEmpty())
    {
        str = "idle";
        return;
    }

    if(str == "0")
    {
        str = "N0";
        return;
    }

    int num = atoi((LPCTSTR)str);
    if(num>0 && num<=36){
        str.Format("N%d", num);
    }
}

void CDlgPick::RefreshRoulette()
{
    HWND hwnd = CRobot::getIntance()->GetHwnd();

    CRect rect;
    ::GetWindowRect(hwnd, rect);
    ClientToScreen(&rect);
    ::InvalidateRect(hwnd, &rect, TRUE);
}

void CDlgPick::OnGet() 
{
    CString str;
	GetDlgItemText(IDC_N, str);
    ReFormatBetName(str);

    POINT pos;

    // for read profile
    pos = Config::getInstance()->GetPos((LPCTSTR)str);
    SetDlgItemInt(IDC_READ_X, pos.x);
    SetDlgItemInt(IDC_READ_Y, pos.y);

    COLORREF color= Config::getInstance()->GetColor((LPCTSTR)str);
	SetDlgItemInt(IDC_READ_COLOR, color);
 
    // Get color from screen
    pos.x = GetDlgItemInt(IDC_CURRENT_X);
    pos.y = GetDlgItemInt(IDC_CURRENT_Y);
    if(pos.x>0 && pos.y>0)
    {
        COLORREF color = CRobot::getIntance()->SolidAt(pos);
        SetDlgItemInt(IDC_CURRENT_COLOR, color);
    }
    else
    {
        // if pos.x and pox.y ==0
        pos.x = GetDlgItemInt(IDC_READ_X);
        pos.y = GetDlgItemInt(IDC_READ_Y);
        color = CRobot::getIntance()->SolidAt(pos);
        SetDlgItemInt(IDC_CURRENT_COLOR, color);
    }
}

void CDlgPick::OnSet() 
{
	CString str;
	GetDlgItemText(IDC_N, str);
    SetDlgItemText(IDC_N, "");
    ReFormatBetName(str);
    const char *lpszKey = (LPCTSTR)str;

    /*
       write to profile
    */

    // write Position
    POINT pos;
    pos.x = GetDlgItemInt(IDC_CURRENT_X);
    pos.y = GetDlgItemInt(IDC_CURRENT_Y);
    SetDlgItemText(IDC_CURRENT_X, "");
    SetDlgItemText(IDC_CURRENT_Y, "");
    if(pos.x>0 && pos.y>0)
    {
        SetDlgItemInt(IDC_READ_X, pos.x);
        SetDlgItemInt(IDC_READ_Y, pos.y);
        Config::getInstance()->SetPos(lpszKey, pos);

        COLORREF color;

        // write color
        CString strColor;
        GetDlgItemText(IDC_CURRENT_COLOR, strColor);
        if(strColor.IsEmpty())
        {
            SetDlgItemText(IDC_READ_COLOR, "");
        }
        else
        {
            color = GetDlgItemInt(IDC_CURRENT_COLOR);
            SetDlgItemInt(IDC_READ_COLOR, color);
            SetDlgItemText(IDC_CURRENT_COLOR, "");
            Config::getInstance()->SetColor(lpszKey, color);
        }
    }
}

void CDlgPick::OnPush() 
{
    Button button;
    
    CString str;
    GetDlgItemText(IDC_CURRENT_X, str);
    if(str.IsEmpty())
    {
        button.x = GetDlgItemInt(IDC_READ_X);
        button.y = GetDlgItemInt(IDC_READ_Y);
    }
    else
    {
        button.x = GetDlgItemInt(IDC_CURRENT_X);
        button.y = GetDlgItemInt(IDC_CURRENT_Y);
    }

    button.Push(CRobot::getIntance()->GetHwnd());
}

void CDlgPick::OnPop() 
{
    Button button;
    
    CString str;
    GetDlgItemText(IDC_CURRENT_X, str);
    if(str.IsEmpty())
    {
        button.x = GetDlgItemInt(IDC_READ_X);
        button.y = GetDlgItemInt(IDC_READ_Y);
    }
    else
    {
        button.x = GetDlgItemInt(IDC_CURRENT_X);
        button.y = GetDlgItemInt(IDC_CURRENT_Y);
    }

    button.Pop(CRobot::getIntance()->GetHwnd());	
}
